﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Configuration;

public partial class mobile_register : System.Web.UI.Page
{
    public string all_openid;
    private static string key = "TEST2013";
    public static string DESencode(string str)
    {
        try
        {
            byte[] rgbkey = Encoding.UTF8.GetBytes(key.Substring(0, 8));
            byte[] rgbIV = rgbkey;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(str);
            DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
            MemoryStream mstream = new MemoryStream();
            CryptoStream cstream = new CryptoStream(mstream, dCSP.CreateEncryptor(rgbkey, rgbIV), CryptoStreamMode.Write);
            cstream.Write(inputByteArray, 0, inputByteArray.Length);
            cstream.FlushFinalBlock();
            cstream.Close();
            return Convert.ToBase64String(mstream.ToArray());
        }
        catch
        {
            return str;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        all_openid = this.Request.Params["openid"].ToString();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "" || TextBox4.Text == "" || TextBox5.Text == "" || TextBox6.Text == "" || TextBox1.Text == null || TextBox4.Text == null || TextBox5.Text == null || TextBox6.Text == null)
        {
            Page.RegisterStartupScript("ss", "<script>alert('请填写所有必需信息!')</script>");
            return;
        }
        string input_name = TextBox1.Text;
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
        //检查用户名是否已经存在
        SqlCommand com_check = new SqlCommand();
        com_check.Connection = sc;
        com_check.CommandText = "select IND_ID from INFO_IND where IND_NAME=@name";
        com_check.Parameters.AddWithValue("@name",input_name);
        if (com_check.ExecuteScalar() == null)
        {
            if (TextBox5.Text.Equals(TextBox6.Text) == true)//用户名可以注册后，验证密码和确认密码一致
            {
                SqlCommand com1 = new SqlCommand();
                com1.Connection = sc;
                com1.CommandText = "select Max(IND_ID) from INFO_IND";
                while (Application["sqllock"] == "lock")
                {//检测是否已经锁住               
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁 
                string max_id = com1.ExecuteScalar().ToString();//获取数据库中最大ID  
                Application["sqllock"] = "unlock";//解锁 
                max_id = max_id.Remove(0, 1);
                int max_id1 = Convert.ToInt32(max_id) + 1;
                string new_id = "I" + max_id1.ToString();
                string new_ivicode = DESencode(new_id);
                string new_openid = all_openid;
                SqlCommand com_insert = new SqlCommand();
                com_insert.Connection = sc;
                com_insert.CommandText = "insert into INFO_IND values (@id,@name,@email,@ivicode,@crd,@openid,@psw)";
                com_insert.Parameters.AddWithValue("@id",new_id);
                com_insert.Parameters.AddWithValue("@name",input_name);
                com_insert.Parameters.AddWithValue("@email",TextBox4.Text);
                com_insert.Parameters.AddWithValue("@ivicode",new_ivicode);
                com_insert.Parameters.AddWithValue("@crd",0);
                com_insert.Parameters.AddWithValue("@openid",new_openid);
                com_insert.Parameters.AddWithValue("@psw",TextBox5.Text);
                //将用户信息插入数据库INFO_IND表
                while (Application["sqllock"] == "lock")
                {//检测是否已经锁住               
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁 
                
               
                if (com_insert.ExecuteNonQuery() == 1)
                { 
                    Application["sqllock"] = "unlock";//解锁 
                    sc.Close();
                    Page.RegisterStartupScript("ss", "<script>alert('注册成功!')</script>");
                }
                else
                {
                    Application["sqllock"] = "unlock";//解锁 
                    sc.Close();
                    Page.RegisterStartupScript("ss", "<script>alert('数据库异常，请重新注册!')</script>");
                }
                
            }
            else
            {
                sc.Close();
                Page.RegisterStartupScript("ss", "<script>alert('密码不正确!')</script>");
            }  
        }
        else 
        { 
            sc.Close();
            Page.RegisterStartupScript("ss", "<script>alert('用户名已存在!')</script>"); 
        }
        
    }
}